package com.kaoyaya.delivery.adapter;

import java.util.List;

import com.kaoyaya.delivery.activity.R;
import com.kaoyaya.delivery.data.entity.Product;
import com.kaoyaya.util.StringUtil;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 产品Adapter
 * @date 2013-5-26
 * @author Goven
 * @email gxl3999@gmail.com
 */
public class ProductAdapter extends BaseAdapter {

	private List<Product> products;
	private LayoutInflater inflater;
	private View.OnClickListener clickListener;
	
	public ProductAdapter(Context context, View.OnClickListener clickListener, List<Product> products) {
		inflater = LayoutInflater.from(context);
		this.products = products;
		this.clickListener = clickListener;
	}
	
	public void addProducts(List<Product> products) {
		this.products.addAll(products);
	}
	
	@Override
	public int getCount() {
		return products.size();
	}

	@Override
	public Product getItem(int arg0) {
		return products.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		Product product = getItem(position);
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_product_detail, null);
			holder.llCategory = (LinearLayout) convertView.findViewById(R.id.llCategory);
			holder.tvCategoryName = (TextView) convertView.findViewById(R.id.tvCategoryName);
			holder.btnBuy = (Button) convertView.findViewById(R.id.btnBuy);
			holder.btnBuy.setTag(product);
			holder.btnBuy.setOnClickListener(clickListener);
			holder.tvArea = (TextView) convertView.findViewById(R.id.tvArea);
			holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
			holder.tvNo = (TextView) convertView.findViewById(R.id.tvNo);
			holder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
			holder.tvSubjectName = (TextView) convertView.findViewById(R.id.tvSubjectName);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tvArea.setText(product.getArea());
		holder.tvName.setText(product.getName());
		holder.tvNo.setText(String.valueOf(product.getId()));
		holder.tvPrice.setText(String.valueOf(product.getPrice()));
		if (StringUtil.isTrimBlank(product.getCategoryName())) {
			product.setCategoryName("null");
		}
		holder.tvCategoryName.setText(product.getCategoryName());
		holder.tvSubjectName.setText(product.getSubjectName());
		if (position != 0 && product.getCategoryName().equals(getItem(position - 1).getCategoryName())) {
			holder.llCategory.setVisibility(View.GONE);
		} else {
			holder.llCategory.setVisibility(View.VISIBLE);
		}
		return convertView;
	}

	class ViewHolder {
		LinearLayout llCategory;
		TextView tvNo, tvName, tvPrice, tvSubjectName, tvArea, tvCategoryName;
		Button btnBuy;
	}
	
}
